#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

#include "Utils.h"

//static char sourceHeaderName[80] = "../DABHomework1.h";
//static char targetHeaderName[80] = "Homework1.h";
static char assDir[80] = "../Ass1";

using namespace std;

int main(int argc, char *argv[] )
{
  char inLine[256];
  char s[256];
  int systemReturn;
  unsigned int pos;
  string command;
  string fileName, login, theString;
  string inFileName;
  string outFileName;
  string logFileName;

  Utils::checkArgs(3, argc, argv, "infilename outfilename logfilename");
  inFileName = static_cast<string>(argv[1]);
  outFileName = static_cast<string>(argv[2]);
  logFileName = static_cast<string>(argv[3]);

  Utils::OutFileOpen(outFileName);
  Utils::LogFileOpen(logFileName);

  Utils::InFileOpen(inFileName);
  Utils::logStream << endl << "Read the file" << endl; 
  while( !(Utils::inStream.getline(inLine, 80)).eof())
  {
    Utils::logStream << inLine << endl;
    theString = static_cast<string>(inLine);

    pos = theString.find("_");
    login = "";
    if((pos >= 0) && (pos <= theString.length()))
    {
      login = theString.substr(0,pos);
      Utils::logStream << "login: " << login << endl;
    }
    else
    {
      Utils::logStream << "BAD LOGIN POS: " << pos << endl;
    }

    pos = theString.rfind("_");
    fileName = "";
    if((pos >= 0) && (pos <= theString.length()))
    {
      fileName = theString.substr(pos+1);
      Utils::logStream << "fileName: " << fileName << endl;

      sprintf(s, "mkdir %s/%s\n", assDir, login.c_str());
      Utils::logStream << s;
      systemReturn = system(s);

//      sprintf(s,"cp %s %s/%s/%s\n",
//              sourceHeaderName, assDir, login.c_str(), targetHeaderName);
      Utils::logStream << s;
      systemReturn = system(s);

      sprintf(s,"cp ../Submissions/%s %s/%s/%s\n",
                 inLine, assDir, login.c_str(), fileName.c_str());
      Utils::logStream << s;
      systemReturn = system(s);
    }
    else
    {
      Utils::logStream << "BAD FILENAME POS: " << pos << endl;
    }
  }
  Utils::FileClose(Utils::inStream);

  Utils::logStream << "Done" << endl << endl << endl; 

  return 0;
} // int main( )

